package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;
import com.campusrecruitment.pojo.recruitMessage;
import com.campusrecruitment.pojo.resume;
import com.campusrecruitment.pojo.resumeStatus;
import com.campusrecruitment.service.resumeStatusService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生简历管理
 */
@Slf4j
@RestController
@RequestMapping("/resume")
public class resumeController {

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 修改简历信息
     * @param resume
     * @return
     */
    @PostMapping("/update")
    public R<String>update(@RequestBody resume resume) throws JsonProcessingException {
        String studentName = resume.getName();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resume.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有简历信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });

        for (int i=0;i<resumes.size();i++
        ) {
            if (resumes .get(i).getPosition().equals(resume.getPosition())            //比对岗位
                    && resumes .get(i).getName().equals(resume.getName())) {    //比对学生姓名
                //更新该用户
                resumes.get(i).setPosition(resume.getPosition());
                resumes.get(i).setGraduation(resume.getGraduation());
                resumes.get(i).setTechnologyStack(resume.getTechnologyStack());
                break;
            }
        }

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes);
        jsonUtils.writeJson(jsonString);

        return R.success("修改成功");
    }

    /**
     * 查询全部学生个人简历信息
     * @return
     */
    @PostMapping("/list")
    public R<List<resume>>list(@RequestBody resume resume) throws JsonProcessingException {
        String studentName = resume.getName();

        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resume.json"));
        String jsonList = jsonUtils.readJson(String.class);

        //读出所有简历信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });
        //使用集合得到简历数据
        List<resume> list = new ArrayList<>();
        for (resume r : resumes
        ) {
            if (r.getName().equals(studentName)) {
                list.add(r);
            }
        }
        return R.success(list);
    }

    /**
     * 新增简历
     * @param resume
     * @return
     */
    @PostMapping("/add")
    public R<String>addResume(@RequestBody resume resume) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resume.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });

        resumes.add(resume);

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes);
        jsonUtils.writeJson(jsonString);

        return R.success("添加成功");
    }

    /**
     * 删除简历
     * @param resume
     * @return
     * @throws JsonProcessingException
     */
    @DeleteMapping
    public R<String>daleteResume(@RequestBody resume resume) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resume.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<resume> resumes = mapper.readValue(jsonList, new TypeReference<List<resume>>() {
        });
        for (int i=0;i<resumes .size();i++
        ) {
            if (resumes .get(i).getPosition().equals(resume.getPosition())            //比对岗位
                    && resumes .get(i).getName().equals(resume.getName())) {    //比对学生姓名
                //删除该用户
                resumes .remove(resumes .get(i));
                break;
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(resumes);
        jsonUtils.writeJson(jsonString);

        return R.success("删除成功！");
    }

    @PostMapping("/resumeStatusList")
    public R<List<resumeStatus>>resumeStatusList(@RequestBody resumeStatus resumeStatus) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\resumeStatus.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //读出所有招聘信息
        List<resumeStatus> resumeStatuses = mapper.readValue(jsonList, new TypeReference<List<resumeStatus>>() {
        });

        List<resumeStatus> list=new ArrayList<>();
        for (resumeStatus r : resumeStatuses
        ) {
            if (r.getName().equals(resumeStatus.getName())) {
                list.add(r);
            }
        }
        return R.success(list);
    }


}
